package com.archermind.boc.dal.business.impl;

import java.util.HashMap;
import java.util.List;

import com.archermind.boc.Logger;
import com.archermind.boc.dal.business.ISocketException;
import com.archermind.boc.dal.business.service.BizService;
import com.archermind.boc.dal.business.support.DispatchSupport;
import com.archermind.boc.domain.Biz;
import com.archermind.boc.domain.BizStatus;
import com.archermind.boc.domain.IQ;
import com.archermind.boc.domain.Session;
import com.archermind.boc.ehcache.UserCache;
import com.archermind.boc.ehcache.UserEhcacheImpl;
import com.archermind.boc.ehcache.UserStatus;
import com.archermind.boc.socket.out.IBocSocketWriter;

import com.archermind.sessionpool.BizSessionPool;

public class SocketExceptionImpl implements ISocketException{
	private BizService bziService;
	private UserEhcacheImpl userEhcache;
	private IBocSocketWriter bocSocketWriter;
	private Logger log = Logger.getLogger(SocketExceptionImpl.class.getName());
	public BizService getBziService() {
		return bziService;
	}
	public void setBziService(BizService bziService) {
		this.bziService = bziService;
	}
	public UserEhcacheImpl getUserEhcache() {
		return userEhcache;
	}
	public void setUserEhcache(UserEhcacheImpl userEhcache) {
		this.userEhcache = userEhcache;
	}
	@SuppressWarnings("static-access")
	public void stocketException(String sessionID) {
		// TODO Auto-generated method stub
		UserCache userCache = userEhcache.getUserCachebySessionId(sessionID);
		log.info("退出端的sessionid========="+sessionID);
		if(userCache!=null)
		{
			if(userCache.getBiz()!=null)
			{
				String ontherSessionID = Transmit.transmitUser(sessionID);
				IQ iq = new IQ();
				iq.setSessionID(ontherSessionID);
				Session session = new Session();
				HashMap<String, List<String>> map = new HashMap<String, List<String>>();
				DispatchSupport.setMap("status", map, BizStatus.BIZSTATUS_EXCEPTION);
				session.setX(map);
				iq.setSession(session);
				bocSocketWriter.write(iq);
				BizSessionPool pool = new BizSessionPool();
				pool.removeBiz(userCache.getBiz().getBizid());
				Biz biz = bziService.getBizById(userCache.getBiz().getBizid());
				biz.setLog(biz.getLog()+"流水号 :"+biz.getBizid()+BizStatus.BIZSTATUS_EXCEPTION);
				biz.setStatus(BizStatus.BIZSTATUS_EXCEPTION);
				bziService.modifyBiz(biz);
				log.info("另一端的sessionid=========="+ontherSessionID);
				userEhcache.removeElementById(userCache.getUserid());
				UserCache ontheruserCache = userEhcache.getUserCachebySessionId(ontherSessionID);
				if(ontheruserCache != null)
				{
					ontheruserCache.setBiz(null);
					ontheruserCache.setIsBusy(UserStatus.USER_LEISURE);
					userEhcache.modifyUserCache(ontheruserCache);
					log.info("---------------------------退出成功!-------------------------");
				}
				else
				{
					log.error("SocketExceptionImpl:ontheruserCache is null");
				}
			}
			else
			{
				userEhcache.removeElementById(userCache.getUserid());
			}
			
		}
		else
		{
			log.info("SocketExceptionImpl:userCache is null");
		}
		
	}
	public IBocSocketWriter getBocSocketWriter() {
		return bocSocketWriter;
	}
	public void setBocSocketWriter(IBocSocketWriter bocSocketWriter) {
		this.bocSocketWriter = bocSocketWriter;
	}

}
